package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.pojo.Power;
import com.woniu.pojo.Role;
import com.woniu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;
    @ResponseBody
    @RequestMapping("add")
    public Map<String, Object> add(Role role){
        Map<String,Object> map=new HashMap<>();
        try {
            if(role.getRoleName()!=null&&getname(role.getRoleName())&&role.getRoleStatus()!=null){
                roleService.add(role);
                map.put("result","ok");
            }else {
                map.put("result","error");
            }
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }
        return map;

    }
    @ResponseBody
    @RequestMapping("deleteByExample")
    public Map<String, Object> deleteByExample(Integer [] roleId){
        Map<String,Object> map=new HashMap<>();
        try {
            roleService.deleteByExample(roleId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("deleteById")
    public Map<String, Object> deleteById(Integer  roleId){
        Map<String,Object> map=new HashMap<>();
        try {
            roleService.deleteById(roleId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("findOne")
    public Map<String, Object>  findOne (Integer  roleId){
        Map<String,Object> map=new HashMap<>();

        try {
            Role one = roleService.findOne(roleId);
            System.out.println(one);
            map.put("role",one);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }
        return map;
    }
    @ResponseBody
    @RequestMapping("findAll")
    public Map<String, Object> findAll (@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue ="10") Integer pageSize,String roleName){


        Map<String,Object> map=new HashMap<>();
        try {
            PageHelper.startPage(pageNum,pageSize);

            List<Role> all = roleService.findAll(roleName);
            PageInfo<Role> pageInfo=new PageInfo<Role>(all);
            map.put("page",pageInfo);
            map.put("roleList",all);
            map.put("result","ok");

        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }

        return map;
    }
    @RequestMapping("role")
    public String role(){
        return "WEB-INF/role/roleList";
    }
    @RequestMapping("addRole")
    public String add(){
        return "WEB-INF/role/addRole";
    }

    //查看角色名称是否重复

    @RequestMapping("updateRole")
    public String updateRole(Integer roleId, Model model){
        System.out.println(roleId);
        model.addAttribute("roleId",roleId);
        return "WEB-INF/role/updateRole" ;
    }
    public boolean getname(String roleName){

        return roleService.issame(roleName);

    }
    @ResponseBody
    @RequestMapping("updateAjax")
    public Map<String, Object> updateAjax(Role role){
        Map<String,Object> map=new HashMap<>();
        try {
            roleService.updateByExample(role);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping("rolePower")
    public String rolePower(Integer roleId,Model model){
       model.addAttribute("roleId",roleId);
        return "WEB-INF/role/rolePower";
    }
    @ResponseBody
    @RequestMapping("rolePowerAjax")
    public List<Power> rolePowerAjax(Integer roleId, Model model){
        List<Power> tree = roleService.getRolePower(roleId);
        model.addAttribute("roleId",roleId);
        return tree;
    }
    @ResponseBody
    @RequestMapping("rolePowerAdd")
    public Map<String,Object> rolePowerAdd(Integer []rp,Integer roleId){
        Map<String,Object> map=new HashMap<>();
        try {
            roleService.rolePowerAdd(rp, roleId);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }


        return map;
    }

}
